DELPHI SOCKETS COMPONENT V3.0

Written by: Gary T. Desrosiers

desrosi@pcnet.com

71062,2754

SOCKETS component provides Delphi with an easy to use interface to Winsock

functionallity. Version 3.0 is a rewrite of the sockets code for Delphi 2.0. Previous

versions of the SOCKETS component work with 16 bit versions of Delphi.

In addition to converting SOCKETS to work with Delphi 2.0, a number of improvements

have been made to the component. Two properties; HostName and MaximumReceiveLength

have been added, the method GetLocalPAddr and an event; OnDataNeeded. In addition to

a number of bug fixes and recommended improvements from suggestions I received.

I still get a lot of questions about how to write servers that handle multiple simultaneous requests. So, in addition to the REXEC and FTP clients, I have included a basic WWW server that can be used with any browser.

The following is a summary of the Version 3.0 improvements;

- Addition of the WWW server example.

- Added properties;

- HostName, Returns the name of the local host.

- MaximumReceiveLength, Sets the maximum receive buffer size.

- Added Methods;

- GetLocalIPAddr, returns the IP address of the local host

- Added Events;

- OnDataNeeded, called when the socket needs data and it's

okay to write.

Files contained in SOCKV3.ZIP

SOCKETS.PAS - The SOCKETS component

SOCKETS.DCR - Component resource file

SOCKETS.WRI - What you're reading now.

REXEC.ZIP - REXEC client sample application

FTP.ZIP - FTP client sample application

CLTSVR.ZIP - Simple client/server example application

WWW.ZIP - Basic World Wide Web server.

Installation

In Delphi, drop down the Component + Install and click the Add button.

Enter the path and file name of where the SOCKETS.PAS module is located.

Click on OK

The SOCKETS component should now appear in the Samples folder within Delphi.

Test it out using one of the supplied applications.

Summary of the implemented properties

Text Retrieves or sends data to connected partner.

Authorized True/False - Client socket number is within authorized (<1024) range.

Peek Gets data without actually receiving data.

OOB Gets urgent (Out Of Band) data.

SocketNumber Gets or sets Socket number.

MasterSocket Gets or sets listener socket number

MaximumReceiveLength Gets or sets the maximum receive buffer size

HostName Gets the local host name.

IPAddr Gets or sets the IP address to Sconnect to.

Port Gets or sets the port name/number to Sconnect or Slisten to.

NonBlocking True/False - Sets the mode of the sockets component.

Timeout Number of seconds that blocking mode sockets functions will timeout.

Summary of the implemented Methods

SConnect Connect to listening server

SListen Listen on Port

SCancelListen Cancel listen request

SAccept Accept client connection

SClose Close sockets

SReceive Receive PChar data

SSend Send PChar data

GetPort Get port of SocketNumber

GetIPAddr Get IP Address of SocketNumber

GetPeerPort Get partner's port assignment

GetPeerIPAddr Get partner's IP Address

GetLocalIPAddr Gets the local host's IP Address


Summary of the implemented Events

OnDataAvailable Called when data is available to be received on the socket

OnSessionAvailable Called when a session is available to be accepted

OnSessionClosed Called when a connection is lost

OnSessionConnected Called when an SConnect completes

OnErrorOccurred Called on error conditions

OnDataNeeded Called when it's okay to send on the socket.

Implemented Properties

IPAddr

Sets the IP Address of the partner that you will

eventually SConnect to. You may specify this as

dotted decimal or a literal name to be converted

via DNS.

examples;

Sockets1.IPAddr := 'desrosi';

Sockets1.IPAddr := '127.0.0.1';

addr := Sockets1.IPAddr;

Port

Sets the Port number of the remote port to connect

to or the local port to listen on depending on

whether you subsequently issue a SConnect or SListen.

This can be specified as a number or a literal name

to be converted via DNS.

examples;

Sockets1.Port := 'echo';

Sockets1.Port := '7';

port := Sockets1.Port;

SocketNumber

Returns (or sets) the socket number of the currently

allocated connection.

example;

sock := Sockets1.SocketNumber;

MasterSocket

Returns (or sets) the master socket number (listener)

example;

msock := Sockets1.MasterSocket;

Text

if set, sends the text to the partner.

if read, receives some text from the partner.

examples;

buffer := Sockets1.Text; (* Receive data *)

Sockets1.Text := 'This is a test'; (* Send Data *)

Peek

Returns data waiting to be received but does not actually

receive the data.

example;

buffer := Sockets1.Peek; (* Look at arriving data *)

OOB

if set, sends the text to the partner as urgent (out of

band) data.

if read, receives urgent (out of band) data.

examples;

buffer := Sockets1.OOB;

Sockets1.OOB := 'ABOR'; (* Send abort urgently *)

NonBlocking

Set to False for blocking mode and True for non-blocking

mode (the default). When the socket is in blocking

mode, none of the event callback functions (with the

exception of OnErrorOccurred) will function.

Timeout

When NonBlocking = 0 (blocking mode) this value

specifies the maximum amount of time that

a socket operation can take. After this time

limit expires, the operation is canceled and

an error occurs. The default is 30 (seconds).

The Valid range is 0-60 seconds. Setting Timeout

to zero causes the operation to wait indefinitely.

Authorized

If set to true, SConnect binds to a port < 1024. For use in

specialized client applications where the server requires

authorized port assignments. The default is false, nonauthorized.

MaximumReceiveLength

Defaults to 8192. Allows you to specify the maximum buffer size a

that will be returned in a;

buff := Sockets1.Text

or

Sockets1.SReceive(Socket,buff,len);

HostName

Returns the name of the local host.


Implemented Methods

SConnect

Connects to the remote (or local) system

specified in the IPAddr and Port properties.

example;

Sockets1.SConnect; (* Connect to partner *)

SListen

Listens on the port specified in the Port property.

example;

Sockets1.SListen; (* Establish server environment *)

SCancelListen

Cancels listens on the socket.

example;

Sockets1.SCancelListen; (* Dont accept further clients *)

SAccept

Accepts a client request. Usually issued in

OnSessionAvailable event.

example;

Sock := Sockets1.SAccept; (* Get client connection *)

SClose

Closes the socket.

example;

Sockets1.SClose; (* Close connection *)

SReceive

Receives data from partner, similar to

reading the property Text although this function

uses PChar instead of Pascal strings.

example;

len := Sockets1.SReceive(Sockets1.SocketNumber,szBuffer,4096);

SSend

Sends data to the partner, similar to

setting the property Text although this function

uses PChar instead of Pascal strings.

example;

len := Sockets1.SSend(Sockets1.SocketNumber,szBuff,32000);

GetPort

Returns the actual port number of the socket

specified as the argument. Generally used when you've

specified a port of zero and need to retrieve the

assigned port number.

GetIPAddr

Returns the IP Address of the socket specified

as the argument.

GetPeerPort

Returns the partners port number of the socket

specified as the argument.

GetPeerIPAddr

Returns partners IP Address of the socket

specified as the argument.

GetLocalIPAddr

Returns the local host's IP Address. This is accomplished by getting

the local host name and then mapping the name to IP. If the host

does not have a name or there is no entry way to map the name to

IP, the call will fail.

Implemented Events

OnDataAvailable

Called when data is available to

be received from the partner. You should issue;

buffer := Sockets1.Text;

or a SReceive method to receive the data from the partner.

OnSessionAvailable

Called when a client has requested

to connect to a 'listening' server. You can call

the method SAccept here.

OnSessionClosed

Called when the partner has closed

a socket on you. Normally, you would close your side

of the socket when this event happens.

OnSessionConnected

Called when the SConnect has

completed and the session is connected. This is a

good place to send the initial data of a conversation.

Also, you may want to enable certain controls that

allow the user to send data on the conversation here.

OnErrorOccurred

Called when an error occurs on the socket.

If defined, the OnErrorOccurred procedure is called when

the error occurs. If the procedure isn't defined then

a dialog box is displayed with the error text and the

program is halted.

OnDataNeeded

Called when an data is needed on the socket. i.e. it is okay to send.

All code including the example programs are released to the public domain and as such can be used in any mannor you see fit.

Gary T. Desrosiers

desrosi@pcnet.com

compuserve: 71062,2754